angular.module("gaOffer").service("offerService", ["$q", "$http", "$cookies", "$location", function ($q, $http, $cookies, $location) {
  this.addOffer = function (offer) {
    console.log(offer);
    //提交表单
    $http.post(serverGwPath+"app/gaoffers/addNewGaOfferByAdmin", offer).then(function (resp) { //无论是否保存成功，都进行页面跳转
      alert(resp.data.data)
      $location.path("/offer/list");
    }, function (resp) {
      console.log("Saved Failly! Status: " + resp.status);
      $location.path("/offer/list");
    });
  };

  this.updateOffer = function (offer) {
    console.log(offer);
    //提交表单
    $http.post(serverGwPath+"app/gaoffers/updateGaOfferByAdmin", offer).then(function (resp) { //无论是否保存成功，都进行页面跳转
      alert(resp.data.data)
      $location.path("/offer/list");
    }, function (resp) {
      console.log("Saved Failly! Status: " + resp.status);
      $location.path("/offer/list");
    });
  };


  this.addOfferConfig = function () {
    return $http.get(serverGwPath+"app/gaoffers/addOfferConfig").then(function (resp) {
      return resp.data;
    }, function (resp) {
      return $q.reject(resp.status);
    });
  }

  this.getOfferById = function (offerId) {
    var params = {
      offerId: offerId,
      os: 3,
      token: $cookies.get('token')
    };
    return $http.post(serverGwPath+"app/gaoffers/getAdminOfferById", params).then(function (resp) { //无论是否保存成功，都进行页面跳转

      console.log("getAdminOfferById: " + angular.toJson(resp.data.data));
      var data = resp.data.data;
      //            $location.path("#/offer/list");
      return data;
    }, function (resp) {
      console.log("Saved Failly! Status: " + resp.status);
      //            $location.path("#/offer/list");
      return $q.reject(resp.status);
    });
  }
}]);